import {createActions, combineActions, handleActions} from 'redux-actions';

const initialState = {confirm: {open: false}};

export const actionCreators = createActions({
  OPEN_CONFIRM: ({target, message, openType = 'ALERT', confirmPayload, cancelPayload}) => ({open: true, message, target, openType, confirmPayload, cancelPayload}),
  CLOSE_CONFIRM: (closePayload) => ({open: false, closePayload}),
  RESOLVE_CONFIRM: () => ({open: false, closePayload: undefined, target: undefined, message: undefined, confirmPayload: undefined, cancelPayload: undefined})
})


const reducer = handleActions({
  [combineActions(
    actionCreators.openConfirm,
    actionCreators.closeConfirm,
    actionCreators.resolveConfirm
  )](state, {payload}) {
    return {
      ...state,
      confirm: {
        ...state.confirm,
        ...payload
      }
    }
  }
}, initialState);

export default reducer;
